import { _decorator, Color, Component, Label, Node, ProgressBar, Tween, tween } from 'cc';
import { JCComponent } from '../JCComponent';
import { gg } from '../../../Game';
import { JCEventDef } from '../m/JCEventDef';

const { ccclass, property } = _decorator;

@ccclass('StageInfoPanel')
export class StageInfoPanel extends JCComponent {
    @property(Label)
    lbScore: Label = null
    @property(Label)
    lbTargetScore: Label = null
    @property(Label)
    lbBase: Label = null
    @property(Label)
    lbMul: Label = null

    @property(Label)
    lbLevel: Label = null
    @property(ProgressBar)
    prg: ProgressBar = null

    score = { vlaue: 0 }
    onInit() {
        gg.eventTarget.on(JCEventDef.stageAddScore, this.onAddScore, this)
        gg.eventTarget.on(JCEventDef.startNewStage, () => { this.score.vlaue = 0 }, this)

    }
    addScoreTween: Tween<any> = null
    onAddScore(oldScore: number, newScore: number) {

        if (this.addScoreTween) {
            this.addScoreTween.stop()
        }
        this.addScoreTween = tween(this.score).to(0.5, { vlaue: this.prop.pokerGame.currentStage.score }).start()
    }

    // finishColor = new Color(174, 247, 255)
    whiteColor = new Color(255, 255, 255)
    update(deltaTime: number) {
        if (this.prop) {
            this.lbScore.string = `${Math.floor(this.score.vlaue)}`
            this.lbTargetScore.string = `${this.prop.pokerGame.currentStage.targetNum}`
            this.lbBase.string = `${this.prop.pokerGame.currentStage.base}`
            this.lbMul.string = `${this.prop.pokerGame.currentStage.mul}`
            this.lbLevel.string = `关卡${this.prop.pokerGame.currentStage.index + 1}`
            this.prg.progress= Math.min(1,Math.floor(this.score.vlaue)/this.prop.pokerGame.currentStage.targetNum)
            // this.lbScore.color = this.score.vlaue >= this.prop.pokerGame.currentStage.targetNum ? this.finishColor : this.whiteColor
        }

    }
    protected onDestroy(): void {
        this.unscheduleAllCallbacks()
    }
}

